package com.example.demo.modules.databaseui.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.util.R;
import com.example.demo.modules.databaseui.entity.Databse;
import com.example.demo.modules.databaseui.service.DatabseService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

import static com.example.demo.common.util.Utils.isNil;

@RestController
@RequestMapping("/databaseui/databse")
public class DatabseController {

    @Autowired
    private DatabseService databseService;


    @GetMapping("/listAll")
    public R listAll() {
        return R.ok().put("result",databseService.list(null));
    }

    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageSize",dataType="String"),
            @ApiImplicitParam(name="pageNum",dataType="String")
    })
    public R page(
            Integer pageSize,Integer pageNum) {
        if (isNil(pageSize)) {
            pageSize = 10;
        }
        if (isNil(pageNum)) {
            pageNum = 0;
        }
        QueryWrapper<Databse> queryWrapper = new QueryWrapper<>();
        return R.ok().put("result",databseService.page(new Page<>(pageNum,pageSize), queryWrapper));
    }
    @PostMapping("/add")
    public R add(@RequestBody Databse databse) {
        return R.ok().put("result",databseService.save(databse));
    }
    @PostMapping("/update")
    public R update(@RequestBody  Databse databse, HashMap<String, Object> updateOptions) {
        QueryWrapper<Databse> queryWrapper=new QueryWrapper<>();
        if(!isNil(databse.getId())){
            queryWrapper.eq("id",databse.getId());
        }else{
            return R.error("id不能为空");
        }
        return R.ok().put("result",databseService.update(databse,queryWrapper));
    }

    /*
    * @desc
    * 输入账号密码 初始化设置数据库
    * 保存session.currentUser
    * */
    @PostMapping("/init")
    public R init(@RequestBody Databse databse) {
        return R.ok().put("result",databseService.save(databse));
    }
}